home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / KEMO_1 / KEMOTEST.C < prev   
C/C++ Source or Header  |  1992-02-09  |  8KB  |  266 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <time.h>
  4. #include "KeMo.h"
  5.  
  6. void AccuracyTest(void);
  7. void ResponseTest(void);
  8. void TimerTest(void);
  9. void ScreenTest(void);
  10. void KeyCodesTest(void);
  11. void ShowHideMBar(void); 
  12.  
  13. /* Main menu */
  14. main()
  15. {
  16.     char ans[20];
  17.  
  18.     printf("Welcome to KeMo's test suite!\n");
  19.  
  20.     KeMoInit(KeMoQuiet + KeMoAltKeys);    /* no beeps on errors (but do show alerts) */
  21.                                         /* also, set keyboard to differentiate between
  22.                                             left and right modifier keys */
  23.     
  24.     while(1) {
  25.         printf("\nThe following tests are available:\n");
  26.         printf("    [A]ccuracy        - measures keyboard and mouse detection accuracy\n");
  27.         printf("                        (uses KeMoAccuracy)\n");
  28.         printf("    [K]eys            - reports symbol for keys pressed (uses KeMoCode2Asc)\n");
  29.         printf("    [M]enus           - show/hide menu bar\n");
  30.         printf("    [R]esponses       - checks detection of up and down calls to KeMoWait\n");
  31.         printf("    [S]creen Refresh  - calls KeMoSynch on every screen refresh for 20 seconds\n");
  32.         printf("                        and reports what should be the monitor's refresh rate\n");
  33.         printf("    [T]imer           - checks the accuracy of the timer against the Mac clock\n");
  34.         printf("    [Q]uit\n");
  35.         printf("\nPlease select one of the above: ");
  36.         fflush(stdout);
  37.         
  38.         fgets(ans, 10, stdin);
  39.         
  40.         switch (toupper(*ans)) {
  41.             case 'A': AccuracyTest(); break;
  42.             case 'K': KeyCodesTest(); break;
  43.             case 'M': ShowHideMBar(); break;
  44.             case 'R': ResponseTest(); break;
  45.             case 'T': TimerTest(); break;
  46.             case 'S': ScreenTest(); break;
  47.             case 'Q': ExitToShell(); break;
  48.             default: printf("Your choice is not recognized.  Please try again.\n");
  49.                     continue;
  50.             }
  51.         }
  52. }
  53.  
  54.  
  55. void ShowHideMBar() 
  56. {
  57.     long err;
  58.     
  59.     if ( (err = KeMoShowMBar()) && (err = KeMoHideMBar()) )
  60.         printf("\n*** Call to menu bar operation returned error code %ld.\n", err);
  61.     return;
  62. }
  63.  
  64. void KeyCodesTest() {
  65.     KeMoParms parms;
  66.     char c;
  67.     long err;
  68.  
  69.     if (err = KeMoSelect(KeMoKey))    {    /* select the keyboard */
  70.         printf("\n*** Couldn't select keyboard only at this time (%ld).\n", err);
  71.         return;
  72.         }
  73.     
  74.     c = 0;
  75.     printf("\nPress one key at a time.  To end, press 'q' or press no keys for 5 seconds.\n");
  76.  
  77.     while (c != 'q' && c != '!') {    /* end with q or timeout (shows up as ! returned
  78.                                         by KeMoCode2Asc) */
  79.                                         
  80.         KeMoWait(KeMoDown, 5000L, &parms);    /* wait for a kepypress up to 5 seconds */
  81.         
  82.         c = KeMoCode2Asc(parms.key);    /* convert the key code to ASCII representation */
  83.         
  84.         if (c < 10)                    /* keypad numbers */
  85.             printf("K%c ", c+'0');
  86.         else if (c < 20)            /* function keys 1-9 */
  87.             printf("F%c ", c-10+'0');
  88.         else if (c < 30)            /* function keys 10-15 */
  89.             printf("F1%c ", c-20+'0');
  90.         else                        /* other key */
  91.             printf("%c ", c);
  92.         fflush(stdout);
  93.         }
  94.     printf("\n\n");
  95.     KeMoReset();        /* undo selection of keyboard only */
  96. }
  97.  
  98. void ScreenTest()
  99. {
  100.     long x;
  101.     long t, i;
  102.  
  103.     if (x = KeMoSync(1)) {
  104.         printf("Can't do screen sync: error %ld\n", x);
  105.         return;
  106.         }
  107.         
  108.     printf("\nThis test takes 20 seconds.  Please be patient.\n");
  109.     
  110.     i = 0;                /* Time is updated once a second by the Mac
  111.                             (it's a low-memory Mac variable) */
  112.     t = Time + 1;
  113.     while (t > Time);    /* wait for a fresh second */
  114.     
  115.     while (t + 20 > Time) {        /* call KeMoSync for exactly 20 seconds */
  116.         KeMoSync(1);
  117.         i++;
  118.         }
  119.         
  120.     printf("This monitor's screen refresh rate is approximately %.1f Hz.\n", i/20.0);
  121.     
  122.     return;
  123. }
  124.  
  125.  
  126. void TimerTest()
  127. {
  128.     long t;
  129.     
  130.     printf("\nThis test runs for 10 seconds.  Press any key in about 7 seconds (at beep).\n");
  131.     printf("If you hear a second beep, you did not press a key in time (press a key anyway\n");
  132.     printf("to stop the test).\n");
  133.     
  134.     t = KeMoTimerTest();        /* this function takes care of this test */
  135.                                 /* it returns microseconds */
  136.     if (t < 0)
  137.         printf("\nThere was an error: %ld.  Perhaps you didn't press the key early enough?\n", t);
  138.     else
  139.         printf("\nTimer accuracy while looking for keyboard: %ld msecs/sec.\n", (long)(t/1000.0 + .5));
  140.     
  141.     printf("\nRunning Delay test (1 second).\n");
  142.     KeMoTimerStart();
  143.     KeMoDelay(1000L);
  144.     t = KeMoTimerStop(KeMoNoCorrection);
  145.     
  146.     printf("A delay of 1000 msecs takes %ld msecs to complete.\n", t);
  147. }
  148.  
  149. void ResponseTest()
  150. {
  151.     long err, i;
  152.     KeMoParms parms;
  153.  
  154.     if (err = KeMoSelect(KeMoKey))        /* select the keyboard */
  155.         printf("\n*** Couldn't select keyboard only at this time (%ld).\n", err);
  156.     else {
  157.         printf("\nThis test calls KeMoWait repeatedly with a 2 second timeout.\n");
  158.         printf("Looking for 5 key down events: "); fflush(stdout);
  159.         for (i = 0; i < 5; i++) {
  160.             KeMoWait(KeMoDown, 2000L, &parms);
  161.             printf("%c-%s ", KeMoCode2Asc(parms.key), (parms.updown == KeMoDown ? "down" : 
  162.                 (parms.updown == KeMoUp ? "up" : 
  163.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  164.             fflush(stdout);
  165.             }
  166.         printf("\n");
  167.  
  168.         printf("Looking for 5 key up events: "); fflush(stdout);
  169.         for (i = 0; i < 5; i++) {
  170.             KeMoWait(KeMoUp, 2000L, &parms);
  171.             printf("%c-%s ", KeMoCode2Asc(parms.key), (parms.updown == KeMoDown ? "down" : 
  172.                 (parms.updown == KeMoUp ? "up" : 
  173.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  174.             fflush(stdout);
  175.             }
  176.         printf("\n");
  177.  
  178.         printf("Looking for 5 key up or down events: "); fflush(stdout);
  179.         for (i = 0; i < 5; i++) {
  180.             KeMoWait(KeMoUpDown, 2000L, &parms);
  181.             printf("%c-%s ", KeMoCode2Asc(parms.key), (parms.updown == KeMoDown ? "down" : 
  182.                 (parms.updown == KeMoUp ? "up" : 
  183.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  184.             fflush(stdout);
  185.             }
  186.         printf("\n");
  187.         }
  188.         
  189.     if (err = KeMoSelect(KeMoMouse))
  190.         printf("\n*** Couldn't select mouse only at this time (%ld).\n", err);
  191.     else {
  192.         printf("Looking for 5 mouse down events: "); fflush(stdout);
  193.         for (i = 0; i < 5; i++) {
  194.             KeMoWait(KeMoDown, 2000L, &parms);
  195.             printf("button-%s ", (parms.updown == KeMoDown ? "down" : 
  196.                 (parms.updown == KeMoUp ? "up" : 
  197.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  198.             fflush(stdout);
  199.             }
  200.         printf("\n");
  201.  
  202.         printf("Looking for 5 mouse up events: "); fflush(stdout);
  203.         for (i = 0; i < 5; i++) {
  204.             KeMoWait(KeMoUp, 2000L, &parms);
  205.             printf("button-%s ", (parms.updown == KeMoDown ? "down" : 
  206.                 (parms.updown == KeMoUp ? "up" : 
  207.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  208.             fflush(stdout);
  209.             }
  210.         printf("\n");
  211.  
  212.         printf("Looking for 5 mouse up or down events: "); fflush(stdout);
  213.         for (i = 0; i < 5; i++) {
  214.             KeMoWait(KeMoUpDown, 2000L, &parms);
  215.             printf("button-%s ", (parms.updown == KeMoDown ? "down" : 
  216.                 (parms.updown == KeMoUp ? "up" : 
  217.                 (parms.updown == KeMoTimedOut ? "timeout" : "???"))));
  218.             fflush(stdout);
  219.             }
  220.         printf("\n");
  221.         }
  222.         
  223.     KeMoReset();
  224.     return;
  225. }
  226.  
  227. void AccuracyTest()
  228. {
  229.     float x;
  230.     short flag;
  231.     long err;
  232.     char ans[20];
  233.  
  234.     printf("\nTest [K]ey or [M]ouse? "); fflush(stdout);
  235.     fgets(ans, 10, stdin);
  236.     
  237.     switch (*ans) {
  238.         case 'k': flag = KeMoKey; break;
  239.         case 'm': flag = KeMoMouse; break;
  240.         default: printf("\nInvalid choice.\n"); return;
  241.         }
  242.     printf("\n%s it is!\n", flag & KeMoAllDevs ? "Both" : 
  243.                             ( flag & KeMoMouse ? "Mouse" : 
  244.                             ( flag & KeMoKey ? "Key" : "Up in the air")));
  245.     
  246.     
  247.     if (err = KeMoSelect(flag)) {
  248.         printf("\n*** Oops! That selection cannot be made at this time (%ld).\n", err);
  249.         return;
  250.         }
  251.     
  252.     printf("End the test by pressing the appropriate device.\n");
  253.     
  254.     x = KeMoAccuracy();    /* accuracy is returned in microseconds */
  255.     
  256.     KeMoReset();
  257.     
  258.     if (x < 0)
  259.         printf("\nThe desired accuracy test could not be performed: error %ld.\n", (long)x);
  260.     else
  261.         printf("\nThe timing accuracy is approximately +/- %0.1f msec.\n\n", 
  262.                     x/1000.0/2.0);
  263.         
  264.     return;
  265. }
  266.